package org.hibernate.cfg.reveng;

import java.util.Map;
import java.util.TreeMap;

import org.hibernate.cfg.Mappings;
import org.hibernate.cfg.reveng.dialect.MetaDataDialect;
import org.hibernate.mapping.Table;

/**
 * 自定义数据库收藏家
 * 将数据库反转信息存入Mappings，并区分表与视图；
 * 
 * @author iPan
 * @version 2013-12-11
 */
public class MappingsDatabaseCollector2 extends MappingsDatabaseCollector {
	
	/** Key为小写字母表名 */
	protected final Map<String, Table> tableMap = new TreeMap<String, Table>();
	/** Key为小写字母视图名 */
	protected final Map<String, Table> viewMap = new TreeMap<String, Table>();

	public MappingsDatabaseCollector2(Mappings mappings, MetaDataDialect metaDataDialect) {
		super(mappings, metaDataDialect);
	}

	@Override
	public Table addTable(String schema, String catalog, String name) {
		throw new RuntimeException("必须指明是数据库表还是视图，该方法禁止使用.");
	}
	
	public Table addTable(String schema, String catalog, String tableName, boolean isTable) {
		Table table = mappings.addTable(quote(schema), quote(catalog), quote(tableName), null, false);
		if (isTable) {
			tableMap.put(tableName.toLowerCase(), table);
		} else {
			viewMap.put(tableName.toLowerCase(), table);
		}
		return table;
	}

	/**
	 * 返回表Map（访问权限由外部控制）.
	 */
	public Map<String, Table> getTableMap() {
		return tableMap;
	}

	/**
	 * 返回视图Map（访问权限由外部控制）.
	 */
	public Map<String, Table> getViewMap() {
		return viewMap;
	}
	
}
